﻿using System;
using System.Html;
using enyo;

namespace JenkinsView
{
    public class DomainModel : enyo.Object
    {
        [EvalAtCompile, NoCompile]
        public const int SVR_LIST_CHANGE = 1;
        [EvalAtCompile, NoCompile]
        public const int JOB_LIST_CHANGE = 2;
        [EvalAtCompile, NoCompile]
        public const int JOB_DETAIL_LIST_CHANGE = 3;
        [EvalAtCompile, NoCompile]
        public const int JOB_BUILDABLE_CHANGE = 4;
        [EvalAtCompile, NoCompile]
        public const int JOB_BUILD_LAUNCH = 5;
        [EvalAtCompile, NoCompile]
        public const int ERROR = -1;

        [EvalAtCompile, NoCompile]
        public const int PANEL_NAVIGATE = 100;
        [EvalAtCompile, NoCompile]
        public const int NEXT_VIEW = 1;
        [EvalAtCompile, NoCompile]
        public const int PREV_VIEW = -1;

        [EvalAtCompile, NoCompile]
        public const int ASYNC_CALL = 101;
        [EvalAtCompile, NoCompile]
        public const int WAIT = 1;
        [EvalAtCompile, NoCompile]
        public const int DONE = -1;

        [EvalAtCompile, NoCompile]
        public const int EDIT_SVR = 110;
        [EvalAtCompile, NoCompile]
        public const int CONFIRM_DEL = 111;


        [EvalAtCompile, NoCompile]
        public const string API_SUFFIX = "api/json?depth=1";

        public static DomainModel Model = new DomainModel();

        private IObserver[] _views = new IObserver[] { };
        public void AddView(IObserver view)
        {
            _views.Push(view);
        }

        public void NotifyAll(int reason, object arg)
        {
            foreach (var v in _views)
            {
                v.Update(reason, arg);
            }
        }

        private ServerModel _svrMgr;
        public ServerModel ServerManager
        {
            get
            {
                if (_svrMgr == null)
                    _svrMgr = new ServerModel();
                return _svrMgr;
            }
        }

        private JobModel _jobMgr;
        public JobModel JobManager
        {
            get 
            {
                if (_jobMgr == null)
                    _jobMgr = new JobModel();
                return _jobMgr;
            }
        }

        private JobDetailModel _jobDetailMgr;
        public JobDetailModel JobDetailManager
        {
            get
            {
                if (this._jobDetailMgr == null)
                    _jobDetailMgr = new JobDetailModel();
                return _jobDetailMgr;
            }
        }

        public void ApplyBasicAuthentication(Ajax ajax)
        {            
            var svr = this._svrMgr.ListCurrentServer();
            if (svr == null)
                return;

            if (svr.User == null || svr.User.Length == 0 || svr.Password == null || svr.Password.Length == 0)
                return;

            Console.Log("Basic Auth.");

            var token = svr.User + ':' + svr.Password;
            var hash = Window.Btoa(token);
            if (ajax.Headers == null)
                ajax.Headers = new { };
            ajax.Headers["Authorization"] = "Basic " + hash;
        }
    }
}
